local StoreManager = class("StoreManager")
local json = require("utils.json")
local FILE_NAME = "storeData.dat"

function StoreManager:ctor()
    self._path = Application.persistentDataPath
    self:_loadData()
end

function StoreManager:_loadData()
    local GameUtils = require("utils.GameUtils")
    local filePath = self._path .. "/" .. FILE_NAME
    local fileText = GameUtils.readFileText(filePath)
    if not fileText then
        self._storeData = {}
    else
        self._storeData = json:decode(fileText)
        if not self._storeData then
            self._storeData = {}
        end
    end
end

function StoreManager:addData(key, data)
    local datas = self._storeData[key] or {}
    table.insert(datas, data)
    self._storeData[key] = datas
end

function StoreManager:setDataList(key, list)
    self._storeData[key] = list
end

function StoreManager:getDataList(key)
    return self._storeData[key] or {}
end

function StoreManager:save()
    local GameUtils = require("utils.GameUtils")
    local filePath = self._path .. "/" .. FILE_NAME
    printDebug(filePath)
    local data = json:encode(self._storeData)
    GameUtils.writeFileText(filePath, data)
end

function StoreManager:saveBattleReport(report)
    local GameUtils = require("utils.GameUtils")
    local filePath = self._path .. "battle.dat"
    printDebug(filePath)
    local data = json:encode(report)
    GameUtils.writeFileText(filePath, data)
end

function StoreManager:getGlobalId(key)
    local datas = self._storeData["globalId"] or {}
    local globalId = datas[key] or 1
    return globalId
end

function StoreManager:setGlobalId(key, globalId)
    local datas = self._storeData["globalId"] or {}
    datas[key] = globalId
    self._storeData["globalId"] = datas
end

return StoreManager
